# -*- coding:utf8 -*- #

# -----------------------------------------------------------------------------------

# ProjectName:   property-ui-auto-test

# FileName:     login_page

# Author:      

# Datetime:    2023/9/6 16:08

# Description：用于埋点测试，用户行为分析，经管菜单点击

# -----------------------------------------------------------------------------------
import json

import timeseries as timeseries

from common.util.baseUtil.base import Base
from time import sleep
import threading
from datetime import datetime


class managementPage(Base):
    # 集团
    def login(self, username="17828071502", password="123123"):
        """
            登录页
        :param username:用户名
        :param password: 密码
        :return: None
        """
        self.send_keys('//*[@id="app"]/div/form/div/div[1]/div/div/input', username)
        self.send_keys('//*[@id="app"]/div/form/div/div[2]/div/div/input', password)
        self.click('//*[@id="app"]/div/form/div/div[3]/div/button')
        sleep(5)

    def tablePipe(self):
        # 工作台进点经管首页
        self.click('//*[@id="qiankun-home"]/div[4]/div[1]/div/div[2]/div[1]/div[2]/div[1]/div/div[2]')
        sleep(5)

    def menu(self):
        # 点击顶部菜单
        self.click('//*[@id="qiankun-home"]/div[1]/section/header/div[1]/div[1]/div/img')
        sleep(0.2)
        # 点击集团经营管理菜单
        self.click('//*[@id="qiankun-home"]/div[5]/div[2]/div/div[1]/div[3]/div[2]/p')

    def click_specific_page(self, xpath):
        # 执行具体的点击操作
        self.menu()
        self.click(xpath)

    def managementHomePage(self):
        # 经管首页
        self.click_specific_page('//*[@id="qiankun-home"]/div[5]/div[2]/div/div[2]/div/div[1]/div[2]/div')
        sleep(3)

    def manage(self):
        # 即时账单
        self.click_specific_page('//*[@id="qiankun-home"]/div[5]/div[2]/div/div[2]/div/div[2]/div[2]/div')

    def money(self):
        try:
            # 财务管理-充值明细
            self.click_specific_page('//*[@id="qiankun-home"]/div[5]/div[2]/div/div[2]/div/div[4]/div[2]/div')
            # 财务管理-分账明细
            self.click_specific_page('//*[@id="qiankun-home"]/div[5]/div[2]/div/div[2]/div/div[4]/div[3]/div')
            # 财务管理-退费明细
            self.click_specific_page('//*[@id="qiankun-home"]/div[5]/div[2]/div/div[2]/div/div[4]/div[4]/div')
        except:
            self.click('//*[@id="qiankun-home"]/div[5]/div[2]/div/div[2]/div/div[3]/div[2]/div')
            self.click_specific_page('//*[@id="qiankun-home"]/div[5]/div[2]/div/div[2]/div/div[3]/div[3]/div')
            self.click_specific_page('//*[@id="qiankun-home"]/div[5]/div[2]/div/div[2]/div/div[3]/div[4]/div')
            pass

    def alarm(self):
        # 异常告警-告警明细
        try:
            self.click_specific_page('//*[@id="qiankun-home"]/div[5]/div[2]/div/div[2]/div/div[3]/div[2]/div')
        except:
            self.click('//*[@id="qiankun-home"]/div[5]/div[2]/div/div[2]/div/div[4]/div[2]/div')
            pass

    def device(self):
        # 设备管理-表计管理
        self.click_specific_page('//*[@id="qiankun-home"]/div[5]/div[2]/div/div[2]/div/div[5]/div[2]/div')
        # 设备管理-虚拟电表
        self.click_specific_page('//*[@id="qiankun-home"]/div[5]/div[2]/div/div[2]/div/div[5]/div[3]/div')
        # 设备管理-基站管理
        self.click_specific_page('//*[@id="qiankun-home"]/div[5]/div[2]/div/div[2]/div/div[5]/div[4]/div')

    def statistics(self):
        # 统计分析-交易统计
        self.click_specific_page('//*[@id="qiankun-home"]/div[5]/div[2]/div/div[2]/div/div[6]/div[2]/div')
        # 统计分析-电费统计
        self.click_specific_page('//*[@id="qiankun-home"]/div[5]/div[2]/div/div[2]/div/div[6]/div[3]/div')
        # 统计分析-电量统计
        self.click_specific_page('//*[@id="qiankun-home"]/div[5]/div[2]/div/div[2]/div/div[6]/div[4]/div')
        # 统计分析-收支统计
        self.click_specific_page('//*[@id="qiankun-home"]/div[5]/div[2]/div/div[2]/div/div[6]/div[5]/div')


class itemPage(Base):

    def login(self, username="17828071502", password="123123"):
        """
            登录页
        :param username:用户名
        :param password: 密码
        :return: None
        """
        self.send_keys('//*[@id="app"]/div/form/div/div[1]/div/div/input', username)
        self.send_keys('//*[@id="app"]/div/form/div/div[2]/div/div/input', password)
        self.click('//*[@id="app"]/div/form/div/div[3]/div/button')
        sleep(5)

    def tablePipe(self):
        # 工作台点进经管首页
        self.click('//*[@id="qiankun-home"]/div[4]/div[1]/div/div[2]/div[1]/div[2]/div[1]/div/div[2]')
        sleep(5)

    def menu(self):
        # 顶部导航菜单、项目经营管理
        self.click('//*[@id="qiankun-home"]/div[1]/section/header/div[1]/div[1]/div/img')
        sleep(0.2)
        self.click('//*[@id="qiankun-home"]/div[5]/div[2]/div/div[1]/div[3]/div[2]/p')

    def click_specific_page(self, xpath):
        # 执行具体的点击操作
        self.menu()
        self.click(xpath)

    def managementHomePage(self):
        # 菜单-首页
        self.click_specific_page('//*[@id="qiankun-home"]/div[5]/div[2]/div/div[2]/div/div[1]/div[2]/div')

    def projectInformation(self):
        # 项目信息
        # 表计管理
        self.click_specific_page('//*[@id="qiankun-home"]/div[5]/div[2]/div/div[2]/div/div[2]/div[2]/div')

    def charge(self):
        # 客服收费
        # 人工缴费
        self.click_specific_page('//*[@id="qiankun-home"]/div[5]/div[2]/div/div[2]/div/div[3]/div[2]/div')

    def money(self):
        # 财务管理
        # 充值明细
        self.click_specific_page('//*[@id="qiankun-home"]/div[5]/div[2]/div/div[2]/div/div[4]/div[2]/div')

    def service(self):
        # 物业服务
        # 通知公告
        self.click_specific_page('//*[@id="qiankun-home"]/div[5]/div[2]/div/div[2]/div/div[5]/div[2]/div')

    def statement(self):
        # 统计报表
        # 用电量报表
        self.click_specific_page('//*[@id="qiankun-home"]/div[5]/div[2]/div/div[2]/div/div[6]/div[2]/div')

    def refund(self):
        # 余额退补
        self.click_specific_page('//*[@id="qiankun-home"]/div[5]/div[2]/div/div[2]/div/div[7]/div[2]/div')
        # 点击新建余额退补按钮
        self.click('//*[@id="main-view"]/div/section/section/section/header/div/form/div[6]/div/button[1]/span')
        self.click('/html/body/div[2]/div/div[1]/button/i')
        # 点击新建余额调整按钮
        self.click('//*[@id="main-view"]/div/section/section/section/header/div/form/div[6]/div/button[2]')
        self.click('/html/body/div[3]/div/div[1]/button/i')

    def share(self):
        # 公共分摊
        self.click_specific_page('//*[@id="qiankun-home"]/div[5]/div[2]/div/div[2]/div/div[8]/div[2]/div')

    def alarm(self):
        # 设备告警
        self.click_specific_page('//*[@id="qiankun-home"]/div[5]/div[2]/div/div[2]/div/div[9]/div[2]/div')

    def help(self):
        # 帮助
        self.click_specific_page('//*[@id="qiankun-home"]/div[5]/div[2]/div/div[2]/div/div[10]/div[2]/div')


def writeTxt(txt, sort=1):
    """sort:1 集团
        sort：2 项目"""
    if sort == 1:
        with open('clique_click.txt', 'a') as f:
            f.write(txt)
    if sort == 2:
        with open('item_click.txt', 'a') as f:
            f.write(txt)


def loopClick(user, count, funcs, click_time_dict, sort=1):
    for i in range(count):
        print(f'第{i + 1}次点击所有页面')
        name = 0
        for func in funcs:
            try:
                func()
                t = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                click_time_dict[list(click_time_dict.keys())[name]].append(t)
                name += 1
            except Exception as e:
                print(f'用户{user}第{i + 1}次点击{list(click_time_dict.keys())[name]}失败{e}')
                name += 1
                pass
    sleep(2)
    print(click_time_dict)

    # 将结果输出到控制台和记录到click.txt文件中
    # 将日期字符串转换为datetime对象
    date_dict = {k: [datetime.strptime(d, "%Y-%m-%d %H:%M:%S") for d in v] for k, v in click_time_dict.items()}
    # 计算每天的日期数量
    day_counts = {k: {d.date(): 1 for d in v} for k, v in date_dict.items()}
    # 输出结果
    for key, data in day_counts.items():
        t = f"{key}所有菜单页面 用户{user}点击次数："
        print(t)
        writeTxt(f'{t}\n', sort)
        for date, count in data.items():
            t1 = f"{date}: {count}"
            print(t1)
            writeTxt(f'{t1}\n', sort)


def run(user, psw, count=1):
    """同一用户点击 ： 集团经管页面
    count: 点击次数
    """
    print("------------测试开始------------")
    basic = managementPage()
    basic.login(user, psw)
    click_time_dict = {"经管首页": [], "即时账单": [], "财务管理": [], "异常告警": [], "设备管理": [], "统计分析": []}
    funcs = [basic.managementHomePage, basic.manage, basic.money, basic.alarm, basic.device, basic.statistics]
    loopClick(user, count, funcs, click_time_dict)
    print("------------测试结束------------")
    basic.quit()


def runItem(user, psw, count=1):
    """同一用户点击 ：  项目经管页面
    count: 点击次数
    """
    print("------------测试开始------------")
    basic = itemPage()
    basic.login(user, psw)
    click_time_dict = {"经管首页": [], "项目信息": [], "客服收费": [], "财务管理": [], "物业服务": [], "统计报表": [],
                       "余额退补": [], "公共分摊": [], "设备告警": [], "帮助": []}

    # funcs = [basic.managementHomePage, basic.projectInformation, basic.charge, basic.money, basic.service,
    #          basic.statement, basic.refund, basic.share, basic.alarm, basic.help]

    # 只点余额退补和公共分摊
    funcs = [basic.refund, basic.share]

    loopClick(user, count, funcs, click_time_dict, sort=2)
    print("------------测试结束------------")
    basic.quit()


def morePeopleRunner(i, count=1, func=run):
    """多用户同时点击经管页面
    count: 每个用户轮询点击次数
    """
    threads = []
    for i in range(i):
        t = threading.Thread(target=func, args=(user[i][0], user[i][1], count))
        threads.append(t)
    for t in threads:
        t.start()
    for t in threads:
        t.join()


if __name__ == '__main__':
    # user = [("17828071601", "123123"), ("17828071602", "123123")]
    # basic = itemPage()
    # basic.login(user[0][0], user[0][1])
    # basic.refund()
    # print()
    # basic.tablePipe()
    # basic.managementHomePage()

    """集团"""
    user = [("17828071501", "123123")]
    # user = [("17828071502", "123123"), ("17828071503", "123123")]
    # user = [("17828071502", "123123"), ("17828071503", "123123"), ("17828071504", "123123")]
    # 同一用户多次点击
    run(user[0][0], user[0][1], 2)

    # 多用户同时点击经管页面
    # morePeopleRunner(i=len(user), count=1)

    """项目，包含余额退补和余额调整按钮点击"""
    # user = [("17828071501", "123123")]
    # user = [("17828071601", "123123"), ("17828071602", "123123")]
    # user = [("17828071502", "123123"), ("17828071503", "123123"), ("17828071504", "123123")]
    # 同一用户多次点击
    # runItem(user[0][0], user[0][1], 1000)

    # 多用户同时点击经管页面
    # morePeopleRunner(i=len(user), count=100, func=runItem)
